﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using SaveTheQueen.Actions;

namespace SaveTheQueen.Component
{
    public class GameObject : DrawableGameComponent
    {
        private readonly GameObject root;
        private readonly GameObject parent;
        private readonly List<GameObject> components;
        private readonly List<CoreAction> actions;

        public GameObject(GameObject owner, Game game)
            : base(game)
        {
            components = new List<GameObject>();
            actions = new List<CoreAction>();

            if (owner == null)
            {
                parent = null;
                root = null;
            }
            else
            {
                parent = owner;
                root = owner.root;
                parent.components.Add(this);
            }
        }

        public void Destroy(GameObject target)
        {
            if (target.parent != this)
                throw new Exception("GameObject can destroy only it's sons, use GameObject.DestroySelf() instead");
            else
            {
                components.Remove(target);
                for (int i = 0; i < actions.Count; i++)
                {
                    actions[i].IsRunning = false;
                }
                target.DestroyChildren();
            }
        }
        public void DestroySelf()
        {
            if (this.parent == null)
                throw new Exception("The object cannot be destroyed if it has no father.");
            else
            {
                this.parent.Destroy(this);
            }
        }
        public void DestroyChildren()
        {
            for (int i = 0; i < this.components.Count; i++)
            {
                this.Destroy(this.components[i]);
            }
        }
        public void DestroyAction(CoreAction action)
        {
            this.Actions.Remove(action);
            action.IsRunning = false;
            action.IsLopping = false;
        }

        public override void Initialize()
        {
            base.Initialize();
            for (int i = 0; i < components.Count; i++)
                components[i].Initialize();
            for (int i = 0; i < actions.Count; i++)
                actions[i].Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            for (int i = 0; i < components.Count; i++)
                components[i].Update(gameTime);
            for (int i = 0; i < actions.Count; i++)
                actions[i].Update(gameTime);
            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {
            for (int i = 0; i < components.Count; i++)
            {
                components[i].Draw(gameTime);
            }
            base.Draw(gameTime);
        }

        public GameObject Root
        {
            get { return root; }
        }
        public GameObject Parent
        {
            get { return parent; }
        }
        public List<GameObject> Components
        {
            get { return components; }
        }
        public List<CoreAction> Actions
        {
            get { return actions; }
        }
    }
}
